package slowka;

import java.beans.*;
import java.util.*;
import javax.swing.JTable;
import javax.swing.table.*;

public class Suite extends XMLSerializable {
    public Suite(String N, String L1, String L2) {
        name = N;
        fromLanguage = L1;
        toLanguage = L2;
    }

    /**
     * Creates new Suite
     * @param N name of suite
     * @param L1 Language 1
     * @param L2 Language 2
     * @param T Set of translations
     */
    public Suite(String N, String L1, String L2, HashSet<Translation> T) {
        name = N;
        fromLanguage = L1;
        toLanguage = L2;
        translations = T;
    }

    /**
     * Creates Suite from a table filled with words
     * @param N name of the suite
     * @param T Table with words
     */
    public Suite(String N, JTable T) {
        if(T.isEditing())
            T.getCellEditor().stopCellEditing();
        
        name = N;
        fromLanguage = T.getColumnName(0);
        toLanguage = T.getColumnName(1);
        
        for(int i = 0; i != T.getRowCount(); i++) {
            addPair(new Word((String)T.getValueAt(i, 0), fromLanguage), new Word((String)T.getValueAt(i, 1), toLanguage));
        }
    }

    /**
     * Adds pair of words
     * @param W1 First word
     * @param W2 Second word
     * @return false if languages are different
     */
    public boolean addPair(Word W1, Word W2) {
        if(W1.isIncomplete() || W2.isIncomplete())
            return false;
        if(!W1.getLanguage().contentEquals(fromLanguage) || !W2.getLanguage().contentEquals(toLanguage))
            return false;

        return translations.add(new Translation(W1, W2));
    }
    
    public boolean checkMeaning(Word W1, Word W2) {
        return translations.contains(new Translation(W1, W2));
    }

    public String getName() {
        return name;
    }
    public String getFromLanguage() {
        return fromLanguage;
    }
    public String getToLanguage() {
        return toLanguage;
    }
    public HashSet<Translation> getTranslations() {
        return translations;
    }

    private TableModel prepareTableModel() {
        DefaultTableModel Model = new DefaultTableModel();

        Model.addColumn(fromLanguage);
        Model.addColumn(toLanguage);

        for(Translation Trans : translations) {
            Model.addRow(new Object[] {Trans.getFrom().getWord(), Trans.getTo().getWord()});
        }
        
        return Model;
    }
    public boolean WriteToTable(JTable T) {
        if(!T.getColumnName(0).contentEquals(fromLanguage) ||
           !T.getColumnName(1).contentEquals(toLanguage))
            return false;

        T.setModel(prepareTableModel());

        return true;
    }

    public static void extractPersistanceDelegates(XMLEncoder e) {
        Translation.extractPersistanceDelegates(e);
        e.setPersistenceDelegate(Suite.class,
                new DefaultPersistenceDelegate(new String[] {"name", "fromLanguage", "toLanguage", "translations"}));
    }

    private String name, fromLanguage, toLanguage;
    private HashSet<Translation> translations = new HashSet<Translation>();
}
